package com.biz.primus.ms.tms.init;

import com.biz.primus.model.cat.enums.CatSaleStatusEnum;
import com.biz.primus.model.cat.enums.CatStatus;
import com.biz.primus.model.cat.vo.CatVo;
import com.biz.primus.ms.tms.dao.repository.CatRepository;
import com.biz.primus.ms.tms.service.CatService;

import javax.annotation.PostConstruct;
import javax.transaction.Transactional;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.google.common.collect.Lists.newArrayList;

/**
 * Created by defei on 1/12/17.
 */
@Service
public class InitManager {

    private static final Logger logger = LoggerFactory.getLogger(InitManager.class);

    private final CatRepository catRepository;

    private final CatService catService;

    @Autowired
    public InitManager(CatRepository catRepository, CatService catService) {
        this.catRepository = catRepository;
        this.catService = catService;
    }

    @PostConstruct
    @Transactional
    public void init() {

        if (catRepository.count() == 0) {
            logger.info("开始初始化猫数据...");
            Long catId = 1L;
            for (int i = 0; i < RandomUtils.nextInt(211, 985); i++) {
                CatVo catVo = buildCat(catId++, "随机薛定谔的猫" + i, RandomUtils.nextInt(1, 5), CatSaleStatusEnum.values()[RandomUtils.nextInt(0, CatSaleStatusEnum.values().length - 1)], RandomUtils.nextInt(1, 100) % 3 == 0 ? CatStatus.ALIVE : CatStatus.DEAD);
                catService.save(catVo);
            }
            logger.info("完成初始化猫数据...");
        }
    }

    private CatVo buildCat(Long id, String name, Integer age, CatSaleStatusEnum saleStatus, CatStatus commonStatus) {

        CatVo Cat = new CatVo();
        Cat.setId(id);
        Cat.setName(name);
        Cat.setAge(age);
        Cat.setSaleStatus(saleStatus);
        Cat.setStatus(commonStatus);
        return Cat;
    }
}
